
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
cache-point
Advanced tools
Simple, filesystem-backed memoisation cache. Use to cache the output of expensive operations speeding up future invocations with the same input.
const Cache = require('cache-point')
/* a mock function to simulate a slow remote request */
async function fetchUser (id) {
return new Promise(resolve => {
setTimeout(() => {
resolve({ id, name: 'Layla' })
}, 1000)
})
}
class Users {
constructor () {
this.cache = new Cache({ dir: 'tmp/example' })
}
async getUser (id) {
let user
try {
/* cache.read() will resolve on hit, reject on miss */
user = await this.cache.read(id)
} catch (err) {
if (err.code === 'ENOENT') {
/* cache miss, fetch remote user */
user = await fetchUser(id)
this.cache.write(id, user)
}
}
return user
}
}
async function start () {
console.time('getUser')
const users = new Users()
const user = await users.getUser(1)
console.timeEnd('getUser')
console.log(user)
}
start().catch(console.error)
The first invocation will take 1 second while the remote user is fetched.
$ node example/simple.js
getUser: 1.025s
{ id: 10, name: 'Layla' }
Since the cache is now warm, future invocations will be fast.
$ node example/simple.js
getUser: 17.07ms
{ id: 10, name: 'Layla' }
string
Promise
string
Promise
string
Promise
Promise
Param | Type |
---|---|
[options] | object |
[options.dir] | string |
string
Current cache directory. Can be changed at any time.
Kind: instance property of Cache
Promise
A cache hit resolves with the stored value, a miss rejects with an ENOENT
error code.
Kind: instance method of Cache
Throws:
Param | Type | Description |
---|---|---|
keys | * | One or more values to uniquely identify the data. Can be any value, or an array of values of any type. |
string
A cache hit returns the stored value, a miss returns null
.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * | One or more values to uniquely identify the data. Can be any value, or an array of values of any type. |
Promise
Write some data to the cache. Returns a promise which resolves when the write is complete.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * | One or more values to index the data, e.g. a request object or set of function args. |
content | * | the data to store |
Write some data to the cache with a key.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * | One or more values to index the data, e.g. a request object or set of function args. |
content | * | the data to store |
string
Used internally to convert a key value into a hex checksum. Override if for some reason you need a different hashing strategy.
Kind: instance method of Cache
Param | Type | Description |
---|---|---|
keys | * | One or more values to index the data, e.g. a request object or set of function args. |
Promise
Clears the cache. Returns a promise which resolves once the cache is clear.
Kind: instance method of Cache
Promise
Clears and removes the cache directory. Returns a promise which resolves once the remove is complete.
Kind: instance method of Cache
© 2016-20 Lloyd Brookes <75pound@gmail.com>.
Tested by test-runner. Documented by jsdoc-to-markdown.
FAQs
Simple, filesystem-backed memoisation cache.
The npm package cache-point receives a total of 151,484 weekly downloads. As such, cache-point popularity was classified as popular.
We found that cache-point demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.